<template>
	<mainContainer>
		<!-- 搜索 -->
		<el-form :model="state.queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="角色名称">
				<el-input v-model="state.queryParams.roleName" clearable></el-input>
			</el-form-item>
			<el-form-item label="角色编码">
				<el-input v-model="state.queryParams.roleCode" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage">
					<el-icon> <ele-Search /> </el-icon>搜索</el-button
				>
				<el-button @click="resetQuery">
					<el-icon> <ele-Refresh /> </el-icon>重置</el-button
				>
			</el-form-item>
		</el-form>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button type="primary" v-auth="'upms:sysrole:add'" @click="add">
					<el-icon> <ele-Plus /> </el-icon> 新增
				</el-button>
			</div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<!-- 新增/编辑表单 -->
		<Form ref="formRef" @initPage="initPage"></Form>
		<RoleMenu ref="roleMenuRef" @initPage="initPage"></RoleMenu>
		<!-- table列表 -->
		<el-table v-loading="loading" :data="state.tableData" border>
			<el-table-column prop="roleName" label="角色名称" />
			<el-table-column prop="roleCode" label="角色编码" />
			<el-table-column prop="roleDesc" label="角色描述" />
			<el-table-column prop="createTime" label="创建时间" />
			<el-table-column label="操作" align="center" fixed="right" width="300">
				<template #default="scope">
					<el-button link type="primary" v-auth="'upms:sysrole:edit'" v-if="scope.row.roleCode != 'ROLE_ADMIN'" @click="edit(scope.row)">
						<el-icon> <ele-Edit /> </el-icon> 修改
					</el-button>
					<el-button link type="danger" v-auth="'upms:sysrole:del'" v-if="scope.row.roleCode != 'ROLE_ADMIN'" @click="del(scope.row.id)">
						<el-icon> <ele-Delete /> </el-icon>
						删除
					</el-button>
					<el-button link type="primary" v-auth="'upms:sysrole:add'" v-if="scope.row.roleCode != 'ROLE_ADMIN'" @click="onAuth(scope.row)">
						<el-icon> <ele-Plus /> </el-icon>
						权限
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="state.page.total"
			v-model:currentPage="state.page.currentPage"
			v-model:pageSize="state.page.pageSize"
			@changePage="initPage"
		></pagination>
	</mainContainer>
</template>
<script setup lang="ts">
import { defineAsyncComponent, ref, reactive } from 'vue';
import { getPage, delObj } from '/@/api/upms/sysrole';
import { ElMessageBox, ElMessage } from 'element-plus';
const Form = defineAsyncComponent(() => import('./form.vue'));
const RoleMenu = defineAsyncComponent(() => import('./rolemenu.vue'));
const state = reactive({
	queryParams: {
		roleName: '',
		roleCode: '',
	},
	page: {
		total: 0,
		currentPage: 1,
		pageSize: 10,
	},
	tableData: [],
});
const showSearch = ref(true);
const loading = ref(false);
const formRef = ref();
const roleMenuRef = ref();

const initPage = async () => {
	loading.value = true;
	let params = {
		current: state.page.currentPage,
		size: state.page.pageSize,
	};
	await getPage(Object.assign(params, state.queryParams))
		.then((response: any) => {
			state.tableData = response.data.data.records;
			state.page.total = response.data.data.total;
			loading.value = false;
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 重置搜索表单
 */
const resetQuery = () => {
	state.queryParams = { roleName: '', roleCode: '' };
};
/**
 * 新增按钮
 */
const add = () => {
	formRef.value.initForm();
};
/**
 * 修改按钮
 */
const edit = (row: any) => {
	formRef.value.initForm(row);
};
/**
 * 删除按钮
 */
const del = (id: string) => {
	ElMessageBox.confirm('此操作将删除该角色，是否继续?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		delObj(id)
			.then(() => {
				ElMessage.success('删除成功');
				initPage();
			})
			.catch(() => {});
	});
};
const onAuth = (row: any) => {
	roleMenuRef.value.initRoleMenu(row.id);
};
initPage();
</script>
